package com.gxa.jd.service.impl;


import com.gxa.exception.CustomerException;
import com.gxa.exception.MessageCode;
import com.gxa.jd.interceptor.UserThreadLocal;
import com.gxa.jd.mapper.GoodsMapper;
import com.gxa.jd.pojo.CartItem;
import com.gxa.jd.pojo.Goods;
import com.gxa.jd.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Map;

/**
 * Created by zxd on 2021/1/6
 **/
@Slf4j
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private GoodsMapper goodsMapper;


    public static final String CART="cart:";

    @Override
    public void addGoodsToCart (Long id,Integer num) throws CustomerException {


        Goods goods = goodsMapper.selectByPrimaryKey(id);

        CartItem cartItem = (CartItem) redisTemplate.boundHashOps(CART + UserThreadLocal.get().getId()).get(goods.getId());

        if(!ObjectUtils.isEmpty(cartItem)){
            //1.先判断该商品是否添加到购物车中，如果已经添加，增加数量
            if(cartItem.getNum()+num>goods.getStock()){
                //初步库存不足
                throw new CustomerException(MessageCode.ERROR);
            }
            cartItem.setNum(cartItem.getNum()+num);
            cartItem.setTotalPrice(cartItem.getPrice()*cartItem.getNum());
            //2.放入更新的结果到redis中
            redisTemplate.boundHashOps(CART +  UserThreadLocal.get().getId()).put(id,cartItem);
            return;
        }

        //2.如果没有添加，就创建一个新的购物项
        if(num>goods.getStock()){
            //初步库存不足
            throw new CustomerException(MessageCode.ERROR);
        }

        cartItem=new CartItem();
        cartItem.setNum(num);
        cartItem.setGoodsId(id);
        cartItem.setImage(goods.getImage());
        cartItem.setName(goods.getName());
        cartItem.setPrice(goods.getPrice());

        //设置小计  单价*数量
        cartItem.setTotalPrice(goods.getPrice()*num);

        //3.放入redis中
        redisTemplate.boundHashOps(CART + UserThreadLocal.get().getId()).put(id,cartItem);

    }

    @Override
    public void updateCartItemNum(Long goodsId, Integer num) {

        CartItem cartItem = (CartItem) redisTemplate.boundHashOps(CART + UserThreadLocal.get().getId()).get(goodsId);

        if(!ObjectUtils.isEmpty(cartItem)){
            //1.先判断该商品是否添加到购物车中，如果已经添加，增加数量
            cartItem.setNum(num);
            cartItem.setTotalPrice(cartItem.getPrice()*cartItem.getNum());
            //2.放入更新的结果到redis中
            redisTemplate.boundHashOps(CART +  UserThreadLocal.get().getId()).put(goodsId,cartItem);
        }

    }

    @Override
    public void removeCartItem(Long  goodsId) {
        redisTemplate.boundHashOps(CART +  UserThreadLocal.get().getId()).delete(goodsId);
    }

    @Override
    public void clearCart() {
        redisTemplate.delete(CART+ UserThreadLocal.get().getId());
    }

    @Override
    public CartItem queryUserCartItem(Long  goodsId) throws CustomerException {
        CartItem cartItem = (CartItem) redisTemplate.boundHashOps(CART + UserThreadLocal.get().getId()).get(goodsId);
        if(ObjectUtils.isEmpty(cartItem)){
            throw new CustomerException(MessageCode.ERROR);
        }
         return cartItem;
    }

    @Override
    public Map queryUserCart( ) {
        return redisTemplate.boundHashOps(CART + UserThreadLocal.get().getId()).entries();
    }
}
